package utils.animate
{
	import flash.events.TimerEvent;
	
	import utils.EnterFrameTimer;
	
	import view.panel.AbsPanel;

	public class TweenPanelOpen
	{
		private var timer:EnterFrameTimer;
		private var target:AbsPanel;
		private var devide:int = 6;
		private var isphase1:Boolean;
		private var centerX:Number;
		private var centerY:Number;
		
		public function TweenPanelOpen()
		{
			initTimer();	
		}
		
		private function initTimer():void
		{
			timer = new EnterFrameTimer(40);
			timer.addEventListener(TimerEvent.TIMER,onTimer);
		}
		
		private function onTimer(event:TimerEvent):void
		{
			phase1();
			phase2();
			setCenter();
		}
		
		private function phase1():void
		{
			if (isphase1)
			{
				return;
			}
			var speed:Number = (1.4 - target.scaleX ) /devide;
			target.scaleX += speed;
			target.scaleY += speed;
			if (target.scaleX >= 1.1)
			{
				isphase1 = true;
			}
		}
		
		private function phase2():void
		{
			if (isphase1)
			{
				var speed:Number = (target.scaleX - 1 ) /3;
				target.scaleX -= speed;
				target.scaleY -= speed;
				
				if (target.scaleX < 1.01)
				{
					stop();
					target.scaleX = target.scaleY = 1;
				}
			}
		}
		
		public function setTarget(target:AbsPanel):void
		{
			this.target = target;
			//target.scaleX = target.scaleY = 0.5;
			centerX = target.defaultX + target.bgWidth / 2;
			centerY = target.defaultY + target.bgHeight / 2;
			isphase1 = false;
		}
		
		private function setCenter():void
		{
			target.x = centerX - target.bgWidth * .5 * target.scaleX;
			target.y = centerY - target.bgHeight * .5 * target.scaleX;
		}
		
		public function start():void
		{
			isphase1 = false;
			timer.start();
		}
		
		public function stop():void
		{
			timer.stop();
		}
	}
}